package com.yzf.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.yzf.entity.ContactNote;
import com.yzf.util.db.DBHelp;
import com.yzf.util.db.RowMapper;

public class ContactNoteDao {
	
	private DBHelp<ContactNote> db = new DBHelp<ContactNote>();
	
	public void save(ContactNote contactNote){
		String sql = "insert into t_contactnote (id,content,createtime,contactid,employeeid) values(?,?,?,?,?)";
		db.executeSQL(sql, contactNote.getId(),contactNote.getContent(),contactNote.getCreatetime(),contactNote.getContactid(),contactNote.getEmployeeid());
	}
	public void delete(String id){
		String sql = "delete from t_contactnote where id=?";
		db.executeSQL(sql, id);
	}
	public void update(ContactNote contactNote){
		String sql = "update t_contactnote set content=? createtime=? contactid=? employeeid=? where id=?";
		db.executeSQL(sql, contactNote.getContent(),contactNote.getCreatetime(),contactNote.getContactid(),contactNote.getEmployeeid(),contactNote.getId());
	}
	
	public List<ContactNote> findByCidEid(String contactid,String eid ){
		String sql = "select id,content,createtime,contactid,employeeid from t_contactnote where contactid=? and employeeid=? order by createtime desc limit 3";
		return db.executeQueryToList(sql, new ContactNoteRowMapper(), contactid,eid);
	}
	public List<ContactNote> find(String contactid,String eid ){
		String sql = "select id,content,createtime,contactid,employeeid from t_contactnote where contactid=? and employeeid=? order by createtime desc ";
		return db.executeQueryToList(sql, new ContactNoteRowMapper(), contactid,eid);
	}
	
	public class ContactNoteRowMapper implements RowMapper<ContactNote>{

		@Override
		public ContactNote mapRow(ResultSet rs) throws SQLException {
			
			ContactNote contactNote = new ContactNote();
			contactNote.setId(rs.getString("id"));
			contactNote.setContactid(rs.getString("contactid"));
			contactNote.setContent(rs.getString("content"));
			contactNote.setCreatetime(rs.getString("createtime"));
			contactNote.setEmployeeid(rs.getString("employeeid"));
			
			return contactNote;
		}
		
		
	}

	public ContactNote findById(String id) {
		String sql = "select id,content,createtime,contactid,employeeid from t_contactnote where id=?";
		return db.executeQueryToObject(sql, new ContactNoteRowMapper(), id);
	}
}
